import scrapy
from scrapy import Selector, Request
from scrapy.http import HtmlResponse

from Insta360.items import Insta360Item


class Insta360Spider(scrapy.Spider):
    name = "insta360"
    allowed_domains = ["insta360.com"]
    # start_urls = ["https://www.agedm.org"]


    def __init__(self):
        self.headers = {
            'Cookie': 'fikker-m6wb-M8KB=GtSrEJKLNOkDgbaPKLaBUVSr3tC05Owt; fikker-m6wb-M8KB=GtSrEJKLNOkDgbaPKLaBUVSr3tC05Owt',
            'Host': 'www.agedm.org',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
            'Content-Type': 'text/html; charset=UTF-8'
        }

    # 自定义url请求
    def start_requests(self):
        for page in range(1, 3):
            yield scrapy.Request(
                url=f'https://www.agedm.org/catalog/all-all-all-all-all-%E7%82%B9%E5%87%BB%E9%87%8F-{page}'
            )

    def parse(self, response: HtmlResponse, **kwargs):
        sel = Selector(response)
        div_list = sel.css('#cata_video_list > div > div')
        for i in div_list:
            insta360Item = Insta360Item()
            insta360Item['title'] = i.css('div > div.card-body > h5 > a::text').extract_first()
            insta360Item['status'] = i.css('div > div.card-body > div:nth-child(6)::text').extract_first()
            insta360Item['url'] = i.css('div > div.card-body > h5 > a::attr(href)').extract_first()
            insta360Item['picture'] = i.css('div > div.video_cover > div > a > img::attr(src)').extract_first()
            insta360Item['type'] = i.css('div > div.video_detail_info:nth-child(8)::text').extract_first().strip()
            insta360Item['introduce'] = i.css('div.video_detail_info.desc::text').extract_first().strip()
            if insta360Item['type'].__contains__('热血') or insta360Item['type'].__contains__('战斗'):
                yield scrapy.http.Request(
                    url=insta360Item['url'],
                    method='GET',
                    headers=self.headers,
                    callback=self.parse_information,
                    dont_filter=True,
                    cb_kwargs={
                        'item': insta360Item
                    }
                )
            else:
                continue


    def parse_information(self, response: HtmlResponse, **kwargs):
        insta360Item = kwargs['item']
        sel = Selector(response)
        insta360Item['region'] = sel.css('div > ul > li:nth-child(1) > span.detail_imform_value::text').extract_first()
        return insta360Item